local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent());
local function_list = ServerOHOH.function_list;
local PetTool=_G.CommonOHOH.load.PetTool();

function M.new(self,...)
	return M(self,...);
end;


function M.ctor(self, parent)
	M.super.ctor(self, parent);    
    self.DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
 
    
end;

function M.dtor(self)
	M.super.dtor(self);
end;

function M.UpdateVoidMake(self)
    if self.updateConnect then
        return
    end

    self.updateConnect=self:AddStepUpdate(self.Update,1)
end

function M.GetMakeTime(self)
    return  PetTool.GetVoidMakeTime(self.parent)
end

function M.Update(self)
    local isHadVoidingdata=false
    local isMakingDone=false
    local deleteK={}
    for k,v in pairs(self.DataBase:GetData("pets").voiding.__real) do
            isHadVoidingdata=true
       if os.time()-k>self:GetMakeTime(v) then
            isMakingDone=true

            if self.DataBase:GetData("pets").had[v] then
                self.DataBase:GetData("pets").had[v]=self.DataBase:GetData("pets").had[v]+1
            else
                self.DataBase:GetData("pets").had[v]=1
            end
            table.insert(deleteK,k)
       end
    end
    
    if not isHadVoidingdata then
        self.updateConnect:Disconnect()
        self.updateConnect=nil
        return true
    end

    if  isMakingDone then
        for _,v in pairs(deleteK) do
            self.DataBase:GetData("pets").voiding[v]=nil
        end
    end

    self.parent:SynchDataToClient("pets")

end

function M.VoidClaim(self,timeId,hadBuy)

    local petId=self.DataBase:GetData("pets").voiding.__real[timeId]
    assert(petId,"petId==NULL")
	if not hadBuy then
		local t=self:GetMakeTime()-(os.time()-timeId)
		assert(t<=0,"needTime>0")
	end

    if self.DataBase:GetData("pets").had[petId] then
        self.DataBase:GetData("pets").had[petId]=self.DataBase:GetData("pets").had[petId]+1
    else
        self.DataBase:GetData("pets").had[petId]=1
    end
    self.DataBase:GetData("pets").voiding[timeId]=nil
    self.parent:SynchDataToClient("pets")
    return true
end

-- 参数：TimeId
function M.VoidDone(self,id)
    local petId=self.DataBase:GetData("pets").voiding[id]
    assert(petId,"petId==null")
    if self.DataBase:GetData("pets").had[petId] then
        self.DataBase:GetData("pets").had[petId]=self.DataBase:GetData("pets").had[petId]+1
    else
        self.DataBase:GetData("pets").had[petId]=1
    end
    self.DataBase:GetData("pets").voiding[id]=nil

    self.parent:SynchDataToClient("pets")

end
function M.VoidCraft(self,petId)
    assert(petId,"petId==null")
    local hadPetStore=self.DataBase:GetData("pets").had
    if not hadPetStore[petId] then
        return false
    end

    if PetTool.GetEvolutionByPetId(petId)~=2 then
        return false
    end

    hadPetStore[petId]=hadPetStore[petId]-1
    if hadPetStore[petId]==0 then
        hadPetStore[petId]=nil
    end
    local stime=os.time()
    local newPetId= PetTool.GetUpEvolutionPetIdByPetId(petId)
    self.DataBase:GetData("pets").voiding[stime]=newPetId

    PetTool.UpdateEquip(petId,hadPetStore,self.DataBase:GetData("pets").equip)

    self.DataBase:Execut(function()
	end);
    self.parent:SynchDataToClient("pets")

    --更新装备的宠物
    self.parent.PetEuqipUpdateEvent:Action()

    return stime
end;

--制作虚空，参数：petId,返回值制作的起始时间戳（TimeId)
M:RegistFunction(function_list.void_craft,M.VoidCraft)

-- 领取虚空,参数：TimeId,返回：true or false
M:RegistFunction(function_list.void_claim ,M.VoidClaim)

return M